package com.example.happiness.service.user.Impl;

import com.example.happiness.constant.Message;
import com.example.happiness.mapper.OrderMapper;
import com.example.happiness.pojo.Order;
import com.example.happiness.pojo.generalDTO.OrderStateDTO;
import com.example.happiness.pojo.uservo.UserOrderVO;
import com.example.happiness.service.user.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Service
public class UserOrderServiceImpl implements UserOrderService {

    @Autowired
    OrderMapper orderMapper;

    public void addorder(Order order) {
        order.setState(Message.ONE);
        order.setCreatedate(LocalDateTime.now());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ssSSS");
        String formattedTime = now.format(formatter);
        // 获取UUID并取前8个字符
        String uuid = UUID.randomUUID().toString();
        String shortUuid = uuid.substring(0, 4);
        order.setOrderid("XFWJ"+formattedTime + shortUuid);
        System.out.println(formattedTime + shortUuid);
        orderMapper.insert(order);
    }

    public List<UserOrderVO> getuserorders(String userid) {
        return orderMapper.getuserorders(userid);
    }

    public void updataOrderState(OrderStateDTO orderStateDTO) {
        orderMapper.updataOrderState(orderStateDTO);
    }
}
